//
//  ZJBBranchViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/18.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJBBranchViewController.h"
#import "BranchPartyOrgizationModel.h"
#import "BranchPositionModel.h"
#import "BranchOpenPartyServiceCenterViewController.h"
#import "BranchOpenPartyOrganizationViewController.h"
/** 地图 */
#import "QMapKit.h"
#import "ZJPointAnnotation.h"
#import "LPEBranchListView.h"
@interface ZJBBranchViewController ()<QMapViewDelegate,LPEBranchListDelegate>
/** 当前地图中心点 */
@property (nonatomic , assign) CLLocationCoordinate2D currentCenterPoint;

@property (nonatomic, strong) QMapView *mapView;
/** 标记点数组 */
@property (nonatomic, strong) NSMutableArray *annotationArray;
/**  菜单按钮  */
@property (strong, nonatomic) UIButton  *meunButton;
/**  e支部列表  */
@property (strong, nonatomic) LPEBranchListView  *listView;

@property (nonatomic , strong) UIImageView * centerLogoImageView;


@end

@implementation ZJBBranchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"支部";
    self.meunButton.hidden = YES;
    self.currentCenterPoint = DEFLocation;
  if (self.type == EBranchType){
   
        self.meunButton.hidden = NO;
    }
    
    [self.view addSubview:self.mapView];
    
    [self.mapView addSubview:self.meunButton];
    if (self.type == EBranchType) {
        [self.mapView addSubview:self.centerLogoImageView];
    }
    [self.view addSubview:self.listView];
    self.listView.hidden = YES;
    //    31.226894,121.497116 上海市坐标

    [self.mapView setCenterCoordinate:self.currentCenterPoint animated:YES];
    if (self.type != EBranchType) {
        [self networking_nearbyOrganization];
    }
    
    //显示列表
    self.listView.alpha = 0;
    self.listView.hidden = NO;
    self.mapView.alpha = 0;
    self.listView.alpha = 1;
    self.mapView.hidden = YES;
 
}

#pragma mark - 菜单按钮的点击方法
- (void)action_meunButton{
    self.listView.alpha = 0;
    [UIView animateWithDuration:0.5 animations:^{
        self.listView.hidden = NO;
        self.mapView.alpha = 0;
        self.listView.alpha = 1;
    } completion:^(BOOL finished) {
        self.mapView.hidden = YES;
    }];
    
    
}

#pragma mark - LPEBranchListDelegate
- (void)LPEBranchListView_click_hide:(BOOL)hide{
    self.mapView.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        self.mapView.alpha = 1;
        self.listView.alpha = 0;
    } completion:^(BOOL finished) {
        self.listView.hidden = YES;
        
    }];
    
}


//坐标点
- (void)networking_nearbyOrganization{
    __weak typeof(self) weakSelf = self;
    if (self.type == ServiceType) {
        NSDictionary *dict = @{@"PAGE_NUMBER":@"1",@"SHOW_NUMBER":@"100"};
        [NetWorkingManager postWithUrlString:@"pudong.org.action.LoginInfo[listServiceStation]" Parameter:dict Success:^(id object) {
            NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
            if ([model.STATE isEqualToString:@"20000"]) {//请求成功
                weakSelf.annotationArray = [[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:[object objectForKey:@"ORG_DEPT"]] mutableCopy];
                [weakSelf annotationArrayUpdate];
            }
        } Fail:^(id object) {
            
        }];
    }
    else if (self.type == EBranchType){
        //    获取党组织
        NSDictionary *dict = @{@"PAGE_NUMBER":@"1" , @"SHOW_NUMBER":@"" , @"Z_LAT":[NSString stringWithFormat:@"%lf" , self.currentCenterPoint.latitude] , @"Z_LNG":[NSString stringWithFormat:@"%lf" , self.currentCenterPoint.longitude] , @"JUL" : @""};
        [NetWorkingManager postWithUrlString:@"pudong.org.action.LoginInfo[listDeptForNear]" Parameter:dict Success:^(id object) {
            NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
            if ([model.STATE isEqualToString:@"20000"]) {//请求成功
                [weakSelf.annotationArray removeAllObjects];
                weakSelf.annotationArray = [[NSArray yy_modelArrayWithClass:[BranchPartyOrgizationModel class] json:[object objectForKey:@"ORG_DEPTS"]] mutableCopy];
                [weakSelf annotationArrayUpdate];
            }
        } Fail:^(id object) {
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
            [weakSelf.annotationArray removeAllObjects];
            [weakSelf annotationArrayUpdate];
        }];
    }
    else{
        NSString * mapTypeString = @"";
        if (self.type == CenterType) {
            mapTypeString = @"3";
        }
        else{
            mapTypeString= @"4";
        }
        NSDictionary *dict = @{@"PAGE_NUMBER":@"1",@"SHOW_NUMBER":@"100",@"ORGDEPT_STATUS":mapTypeString};
        [NetWorkingManager postWithUrlString:[NetWorkingURL listConcenterAddressByState] Parameter:dict Success:^(id object) {
            NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
            if ([model.STATE isEqualToString:@"20000"]) {//请求成功
                weakSelf.annotationArray = [[NSArray yy_modelArrayWithClass:[BranchPositionModel class] json:[object objectForKey:@"ORG_DEPTS"]] mutableCopy];
                [weakSelf annotationArrayUpdate];
            }
        } Fail:^(id object) {
            
        }];
    }
}


- (void)annotationArrayUpdate{
    for (QPointAnnotation *annotation in self.mapView.annotations) {
        if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
            [self.mapView removeAnnotation:annotation];
        }
    }
    
    for (int i = 0; i < self.annotationArray.count; i++) {
        ZJPointAnnotation* anno = [[ZJPointAnnotation alloc] init];
        id model1 = self.annotationArray[0];
        if ([model1 isKindOfClass:[BranchPartyOrgizationModel class]]) {
            BranchPartyOrgizationModel *model = self.annotationArray[i];
            anno.coordinate = CLLocationCoordinate2DMake([model.DEPT_DEADDRESS_COORDINATE_X doubleValue],[model.DEPT_DEADDRESS_COORDINATE_Y doubleValue]);
            anno.index = i;
            [self.mapView addAnnotation:anno];
            
        }else if([model1 isKindOfClass:[BranchPositionModel class]]){
            BranchPositionModel *model = self.annotationArray[i];
            anno.coordinate = CLLocationCoordinate2DMake([model.DEPT_DEADDRESS_COORDINATE_X doubleValue],[model.DEPT_DEADDRESS_COORDINATE_Y doubleValue]);
            anno.index = i;
            [self.mapView addAnnotation:anno];
            
        }
    }
}


//设置大头针

- (QAnnotationView *)mapView:(QMapView *)mapView
           viewForAnnotation:(id<QAnnotation>)annotation
{
    //设置复用标识
    static NSString *pointReuseIdentifier = @"pointReuseIdentifier";
    QAnnotationView *annotationView = [self.mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIdentifier];
    if (annotationView == nil) {
        annotationView = [[QAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIdentifier];
    }
    //显示气泡
    [annotationView setCanShowCallout:YES];
    
    if ([annotation isKindOfClass:[ZJPointAnnotation class]]) {
        //设置图标
        if (self.type == EBranchType) {
            [annotationView setImage:[UIImage imageNamed:@"map_danghui"]];
        }
        else{
            [annotationView setImage:[UIImage imageNamed:@"map_zhengdi"]];
        }
        
    }
    return annotationView;
    
}
//点击大头针走的方法（根据View里面的annotation的index值判断）
- (void)mapView:(QMapView *)mapView didSelectAnnotationView:(QAnnotationView *)view{
    if ([view.annotation isKindOfClass:[ZJPointAnnotation class]]) {
        ZJPointAnnotation *annotation = view.annotation;
        //选中状态设置NO。可以连续点击
        [view setSelected:NO];
        
        if (self.type == EBranchType) {
            BranchPartyOrgizationModel *model = self.annotationArray[annotation.index];
            BranchOpenPartyOrganizationViewController *vc = [BranchOpenPartyOrganizationViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
        else{
            BranchPositionModel *model = self.annotationArray[annotation.index];
            BranchOpenPartyServiceCenterViewController *vc = [BranchOpenPartyServiceCenterViewController new];
            vc.model = model;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
}

//地图移动后获取地图中心点的代理
-(void)mapView:(QMapView *)mapView regionDidChangeAnimated:(BOOL)animated gesture:(BOOL)bGesture
{
    if (self.type == EBranchType) {
        self.currentCenterPoint = mapView.region.center;
        [self networking_nearbyOrganization];
    }
    
}




#pragma mark - Setter


#pragma mark - Getters

- (QMapView *)mapView{
    if (_mapView == nil){
        _mapView = [[QMapView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height  - LP_TopHeight)];
        _mapView.showsUserLocation = YES;//显示用户位置
        _mapView.showsCompass = NO;//不显示指南针
        _mapView.userTrackingMode = QMUserTrackingModeNone;
        _mapView.zoomLevel = 16;
        _mapView.delegate = self;
        _mapView.disable3DBuildingEffect = YES;
    }
    return _mapView;
}

- (NSMutableArray *)annotationArray{
    if (_annotationArray == nil){
        _annotationArray = [NSMutableArray array];
    }
    return _annotationArray;
}

- (UIButton *)meunButton{
    if (_meunButton == nil){
        _meunButton = [[UIButton alloc]initWithFrame:CGRectMake(self.view.width - 65 * [UIScreen zj_screenWScale], self.mapView.height - 95 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale], 50 * [UIScreen zj_screenHScale])];
        _meunButton.backgroundColor = [UIColor whiteColor];
        _meunButton.layer.cornerRadius = _meunButton.width / 2;
        _meunButton.layer.borderWidth = 1;
        _meunButton.layer.borderColor = [UIColor zj_colorWithHexString:@"d2d2ca"].CGColor;
        [_meunButton setImage:[UIImage imageNamed:@"btn_caidan"] forState:UIControlStateNormal];
        [_meunButton addTarget:self action:@selector(action_meunButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _meunButton;
}

- (LPEBranchListView *)listView{
    if (!_listView) {
        _listView = [[LPEBranchListView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height  - LP_TopHeight)];
        _listView.delegate = self;
    }
    return _listView;
}


- (UIImageView *)centerLogoImageView
{
    if (!_centerLogoImageView) {
        _centerLogoImageView = [[UIImageView alloc] initWithFrame:CGRectMake((self.view.width - ItemWidth * 40) / 2, (self.view.height  - ItemHeight * 78) / 2, ItemWidth * 40, ItemHeight * 78)];
        _centerLogoImageView.image = [UIImage imageNamed:@"location_center"];
    }
    return _centerLogoImageView;
}

@end
